layui.use('form', function () {
    var form = layui.form;

    form.verify({
        cusName: function (value) {
            var message = '';
            if (value.length < 2 || value.length > 15) {
                return '客户名称不得少于2字符或大于15字符';
            }
            $.ajax({
                url: 'jay/Customer/judgeName',
                async: false,
                data: "cusName=" + value,
                type: 'get',
                dataType: 'JSON',
                success: function (data) {
                    if (data.obj != null || data.obj != 0) {
                        message = "该用户已存在"
                    }
                }
            })
            if (message != '') {
                return message;
            }
        },
        address: function (value) {
            if (value.length < 1) {
                return '地址不能为空';
            }
        },
        phone: [/^1[3456789]\d{9}$/, '电话号码必须为1***********十一位数']
    });

    layui.form.on('submit(hand)', function (data) {
        $.ajax({
            url: 'jay/Customer/addCustomer',
            type: 'post',
            data: JSON.stringify(data.field),
            contentType: 'application/json;charset=UTF-8',
            dataType: 'json',
            success: function (res) {
                if (res.obj == null || res.obj == 0) {
                    alert("添加失败，请稍后再试!");
                } else {
                    alert("添加成功");
                }
                window.parent.location.reload();
                var index = parent.layer.getFrameIndex(window.name);
                layer.close(index);
            }
        })
    })
});